package com.xuecheng.learning.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.model.dto.RecordDto;
import com.xuecheng.commons.model.vo.CourseRecordVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.learning.domain.CourseRecord;
import com.xuecheng.learning.mapper.CourseRecordMapper;
import com.xuecheng.learning.service.CourseRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 选课记录 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-09-23
 */
@Service
public class CourseRecordServiceImpl extends ServiceImpl<CourseRecordMapper, CourseRecord> implements CourseRecordService {

    @Override
    public ResponseResult getRecord(Long coursePubId) {
        //1.获取用户ID
        Long userId = AuthInfoHolder.getUserId();
        //2.构建条件
        LambdaQueryWrapper<CourseRecord> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseRecord::getCourseId,coursePubId);
        qw.eq(CourseRecord::getUserId,userId);
        //3.查询
        CourseRecord courseRecord = this.getOne(qw);
        //4.判断
        if (ObjectUtil.isEmpty(courseRecord)) {
            return ResponseResult.okResult();
        }
        else {
            CourseRecordVo vo = BeanHelper.copyProperties(courseRecord, CourseRecordVo.class);
            return ResponseResult.okResult(vo);
        }
    }

    @Override
    public ResponseResult saveRecord(RecordDto dto) {
        return null;
    }
}
